# -*- coding:utf-8 -*-
# from . import login_manager
from datetime import datetime
from flask import Flask
from flask_login import UserMixin
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:123456@localhost/blog'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)


class Category(db.Model):
    __tablename__= 'category'
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(30), nullable=False, unique=True)
    gmt_created = db.Column(db.DateTime, default=datetime.utcnow)
    gmt_modified = db.Column(db.DateTime, default=datetime.utcnow)
    deleted = db.Column(db.Boolean, default=False)


class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30), nullable=True, unique=True)
    default = db.Column(db.Boolean, default=False)  # 只有一个角色的字段要设为True,其它都为False
    permissions = db.Column(db.Integer)  # 不同角色的权限不同
    users = db.relationship('User', backref='itsrole')  # Role对象引用users,User对象引用itsrole
    gmt_created = db.Column(db.DateTime, default=datetime.utcnow)
    gmt_modified = db.Column(db.DateTime, default=datetime.utcnow)
    deleted = db.Column(db.Boolean, default=False)


class Follow(db.Model):
    __tablename__ = 'follows'
    follower_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)  # 代表关注者,与relationship的follower对应
    followed_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)  # 代表被关注者,与relationship的followed对应
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    gmt_created = db.Column(db.DateTime, default=datetime.utcnow)
    gmt_modified = db.Column(db.DateTime, default=datetime.utcnow)
    deleted = db.Column(db.Boolean, default=False)


class User(UserMixin, db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(30), nullable=True)
    password = db.Column(db.String(50), nullable=True)
    email = db.Column(db.String(50), nullable=True, unique=True)  # 新建一个邮箱字段
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
    password_hash = db.Column(db.String(100), nullable=True)  # 模型中加入密码散列值
    confirmed = db.Column(db.Boolean, default=False)  # 邮箱令牌是否点击
    name = db.Column(db.String(64))  # 用户信息中的昵称
    location = db.Column(db.String(64))  # 用户地址
    about_me = db.Column(db.Text())  # 用户介绍
    member_since = db.Column(db.DateTime, default=datetime.utcnow)  # 注册时间
    last_seen = db.Column(db.DateTime, default=datetime.utcnow)  # 上次访问时间
    posts = db.relationship('Post', backref='author', lazy='dynamic',
                            cascade='all, delete-orphan')  # 一个用户有多条发表，一对多
    followed = db.relationship('Follow', foreign_keys=[Follow.follower_id],  # 该用户关注了其它用户，对于其它用户而言，该用户就是它的追随者(关注者)
                               backref=db.backref('follower', lazy='joined'),  # 对应follower_id
                               lazy='dynamic',
                               cascade='all, delete-orphan')
    followers = db.relationship('Follow', foreign_keys=[Follow.followed_id],  # 该用户的关注者们，对于关注者们而言，关注者们关注了该用户
                                backref=db.backref('followed', lazy='joined'),  # 对应followed_id
                                lazy='dynamic',
                                cascade='all, delete-orphan')
    comments = db.relationship('Comment', backref='author', lazy='dynamic')
    gmt_created = db.Column(db.DateTime, default=datetime.utcnow)
    gmt_modified = db.Column(db.DateTime, default=datetime.utcnow)
    deleted = db.Column(db.Boolean, default=False)


class Comment(db.Model):
    __tablename__ = 'comments'
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.Text)
    body_html = db.Column(db.Text)
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    disabled = db.Column(db.Boolean)
    post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
    author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    gmt_created = db.Column(db.DateTime, default=datetime.utcnow)
    gmt_modified = db.Column(db.DateTime, default=datetime.utcnow)
    deleted = db.Column(db.Boolean, default=False)


class Post(db.Model):
    __tablename__ = 'posts'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.Text)
    body = db.Column(db.Text)
    body_html = db.Column(db.Text)  # 服务器上的富文本处理字段
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
    author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    comments = db.relationship('Comment', backref='post', lazy='dynamic')
    gmt_created = db.Column(db.DateTime, default=datetime.utcnow)
    gmt_modified = db.Column(db.DateTime, default=datetime.utcnow)
    deleted = db.Column(db.Boolean, default=False)


if __name__ == '__main__':
    db.create_all()
